package team.seekers.table.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import team.seekers.table.entity.CourseInfo;
import team.seekers.table.entity.vo.Classtable;
import team.seekers.table.entity.temp.CourseTable;
import team.seekers.table.entity.vo.UpdateVo;
import team.seekers.table.mapper.CourseInfoMapper;
import team.seekers.table.service.CourseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.table.utils.ParseCourse;
import team.seekers.table.utils.RedisUtil;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cbj
 * @since 2021-04-26
 */
@Service
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements CourseInfoService {
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public Map[][] saveCourseInfo(List<CourseTable> courseTables, String userId) {
        Map[][] mapResult = new HashMap[10][7];
        Map map = new HashMap();
        CourseTable courseTable = courseTables.get(0);
        String[][] courses = courseTable.getCourses();
        for(int i = 0;i<10;i++){
            for(int j = 0;j<7;j++){
                String s = courses[i][j];
                String regex = "([（）《》\\u4e00-\\u9fa5A-Za-z\\(\\)I]){1,50}\\([0-9.a-zA-Z\\\\]{1,30}\\)( \\(([\\u4e00-\\u9fa5]){2,4}\\))? \\([\\u4e00-\\u9fa50-9- ,a-zA-Z_]{1,20}\\)";
                Pattern compile = Pattern.compile(regex);
                Matcher matcher = compile.matcher(s);
                while(matcher.find()){
                    String group = matcher.group();
                    Classtable classtable = ParseCourse.parseCourse(group,courseTable.getSchoolYear(), courseTable.getTerm(), courseTable.getSemesterId());
                    map.put(classtable.getId(),classtable);
                }
                mapResult[i][j] = map;
                map = new HashMap();
            }
        }
        Integer semesterId = courseTable.getSemesterId();
        String schoolYear = courseTable.getSchoolYear();
        String term = courseTable.getTerm();
        String msg = JSON.toJSONString(mapResult);
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setUserId(userId);
        courseInfo.setCourseTable(msg);
        courseInfo.setSemesterId(semesterId);
        courseInfo.setSchoolYear(schoolYear);
        courseInfo.setTerm(term);
        baseMapper.insert(courseInfo);
        //存入缓存
        redisUtil.set(userId,msg);
        return mapResult;
    }

    @Override
    public Map[][]  getByOpenId(String userId) {
        QueryWrapper<CourseInfo> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        CourseInfo courseInfo = baseMapper.selectOne(wrapper);
        String courseTable = courseInfo.getCourseTable();
        Map[][] map = JSON.parseObject(courseTable, Map[][].class);
        return map;
    }

    @Override
    public void updateCourse(UpdateVo vo, Map[][] map) {
        boolean isFirst = false;
        String id = vo.getId();
        if(id==null||id.equals("") ){
            id = UUID.randomUUID().toString();
            isFirst = true;
        }
        String userId = vo.getUserId();
        int[] clock = vo.getClock();
        int day = vo.getDay();
        int[] week = vo.getWeek();
        UUID uuid = UUID.randomUUID();
        for(int i = clock[0];i<=clock[clock.length-1];i++){
            Classtable classtable = new Classtable();
            classtable.setName(vo.getName());
            classtable.setPlace(vo.getPlace());
            classtable.setTeacher(vo.getTeacher());
            classtable.setWeek(week);
            if(!isFirst){
                map[i][day].remove(id);
            }
            map[i][day].put(id,classtable);
        }
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setUserId(vo.getUserId());
        courseInfo.setTerm(vo.getTerm());
        courseInfo.setSchoolYear(vo.getSchoolYear());
        courseInfo.setCourseTable(JSON.toJSONString(map));
        courseInfo.setSemesterId(vo.getSemesterId());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        baseMapper.update(courseInfo,wrapper);
    }

    @Override
    public void deleteCourse(UpdateVo vo, Map[][] map) {
        String userId = vo.getUserId();
        String id = vo.getId();
        Integer day = vo.getDay();
        int[] clock = vo.getClock();
        for(int i = clock[0];i<=clock[clock.length-1];i++){
            map[i][day].remove(id);
        }
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setCourseTable(JSON.toJSONString(map));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        baseMapper.update(courseInfo,wrapper);
    }

}
